<?php

namespace App\Console\Commands;

use App\Episode;
use Illuminate\Console\Command;
use App\Show;

class SortTtmjEpisode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sort:ttmjEpisode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '对天天美剧的ep进行归档';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Episode::where('provider','like','%ttmj%')->select(['url_provider','id'])->chunk(500, function ($episodes) {
            foreach ($episodes as $ep) {

                $key_words = $ep->url_provider;
                //var_dump($key_words);
                $key_words = str_ireplace('/meiju/','',$key_words);
                $key_words = str_ireplace('html','',$key_words);
                $key_words = str_ireplace('.',' ',$key_words);
                $key_words = trim($key_words);

                //var_dump($key_words);

                $show = Show::firstOrCreate(['name_en'=>$key_words]);
                $show->timestamps = false;
                $ep->timestamps = false;
                $show->episodes()->save($ep);



            }
        });
    }
}
